package com.campus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.constant.ActivityStatus;
import com.campus.dao.ManagerActivityMapper;
import com.campus.entry.ManagerActivity;
import com.campus.exception.BusinessException;
import com.campus.pojo.dto.ManagerActivityDto;
import com.campus.response.ResultCode;
import com.campus.service.ManagerActivityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ManagerActivityServiceImpl implements ManagerActivityService {
    @Autowired
    private ManagerActivityMapper managerActivityMapper;


    /**
     * 创建活动
     * @param managerActivity
     */
    @Override
    public void saveActivity(ManagerActivity managerActivity) {
        managerActivity.setStatus(ActivityStatus.CREATE_ACTIVITY);
        managerActivityMapper.insert(managerActivity);
    }

    /**
     * 修改编辑活动
     * @param managerActivity
     */
    @Override
    public void updateActivity(ManagerActivity managerActivity) {
        managerActivityMapper.updateById(managerActivity);
    }

    /**
     *
     *发布活动
     * @param managerActivityId
     */
    @Override
    public void changeActivityStatus(Integer managerActivityId, Integer status) {
        LambdaQueryWrapper<ManagerActivity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(ManagerActivity::getId,managerActivityId);
        ManagerActivity managerActivity=managerActivityMapper.selectOne(lambdaQueryWrapper);
        if (Objects.isNull(managerActivity)){
            throw new BusinessException(ResultCode.NO_ACTIVITY);
        }
        managerActivity.setStatus(status);
        updateActivity(managerActivity);
    }

    @Override
    public Integer getActivityStatus(Integer managerActivityId) {
        LambdaQueryWrapper<ManagerActivity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .select(ManagerActivity::getStatus)
                .eq(ManagerActivity::getId,managerActivityId);
        return managerActivityMapper.selectOne(lambdaQueryWrapper).getStatus();
    }

    @Override
    public List<ManagerActivity> listManagerActivity(ManagerActivityDto managerActivityDto) {
        return null;
    }


    /**
     *  活动查询
     *
     * @param managerActivityDto
     * @return {@link List }<{@link ManagerActivity }>
     */
    @Override
    public Page<ManagerActivity> listManagerActivity(ManagerActivityDto managerActivityDto, Integer pageNum, Integer pageSize) {
        // 创建分页对象，传入当前页码和每页条数
        Page<ManagerActivity> page = new Page<>(pageNum, pageSize);

        // 创建查询条件
        LambdaQueryWrapper<ManagerActivity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 根据活动名称进行模糊匹配
        if (managerActivityDto.getActivityName() != null && !managerActivityDto.getActivityName().isEmpty()) {
            lambdaQueryWrapper.like(ManagerActivity::getName, managerActivityDto.getActivityName());
        }

        // 根据活动状态进行筛选
        if (managerActivityDto.getStatus() != null) {
            lambdaQueryWrapper.eq(ManagerActivity::getStatus, managerActivityDto.getStatus());
        }

        // 排除已删除的活动
        lambdaQueryWrapper.ne(ManagerActivity::getStatus, ActivityStatus.DELETE_ACTIVITY);

        // 使用 MyBatis-Plus 的分页查询
        return managerActivityMapper.selectPage(page, lambdaQueryWrapper);
    }


    @Override
    public Long contActivity() {
        LambdaQueryWrapper<ManagerActivity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(ManagerActivity::getStatus,ActivityStatus.DELETE_ACTIVITY);
        return managerActivityMapper.selectCount(lambdaQueryWrapper);

    }

    @Override
    public ManagerActivity getActivityById(Integer id) {
          return managerActivityMapper.selectById(id);
    }
}
